'use strict';
// 学科管理的控制器文件：增删改查 等操作

const xtpl = require('xtpl');
const path = require('path');
const mysql = require('../configs/database.js');
const baseCtr = require('./baseController.js');

module.exports.subjectAdd = function(req,res){
	xtpl.renderFile(
		path.join(__dirname,'../views/admin/subject_add.html'),
		{page:'subAdd'},
		function(err, content){
			res.setHeader('Content-Type','text/html;charset=utf-8');
			res.send(content);
		});
}

module.exports.subAddData = function(req,res){
	var where =`sub_name='${req.body.name}'`;
	mysql.table('subject').field('sub_id').where(where).find().then(function(result){
			if(JSON.stringify(result)=='{}'){
				var data = {
					sub_name: req.body.name,
					sub_score: req.body.score
				}

				mysql.table('subject').add(data).then(function(){
					res.redirect('/admin/subject/subject_list');
					// console.log('添加成功');
				}).catch(function(err){
					console.log(err);
				});
			}else{
				baseCtr.sendMsg('该学科已存在',res);
			}
	}).catch(function(err){
	    console.log(err);	
	});
}

module.exports.subjectList = function(req,res){
	var pn = req.query.pn != undefined ? req.query.pn:1;
	var limit = 3;
	var start = (pn-1)*limit;
	mysql.table('subject').limit(start,limit).select().then(function(data){
		mysql.table('subject').count('sub_id').then(function(total){
			var pageArr = [];
			var totalPn = Math.ceil(total/limit);
			for(var i=1; i<=totalPn; i++){
				pageArr.push(i);
			}
			xtpl.renderFile(
				path.join(__dirname,'../views/admin/subject_list.html'),
				{
					page:'subList',
					subList: data,
					pageArr: pageArr,
					pn: parseInt(pn),
					endPn: totalPn
				},
				function(err, content){
					res.setHeader('Content-Type','text/html;charset=utf-8');
					res.send(content);
				}
			);
		});
	}).catch(function(err){
		console.log(err);
	})
}

module.exports.subDelData = function(req,res){
	var id = req.params.id;
	mysql.table('subject').where(`sub_id='${id}'`).delete().then(function (affectRows) {
	    res.redirect('/admin/subject/subject_list');
	});
}

module.exports.subDelAllData = function(req,res){
	// console.log(req.params.id);
	var where = `sub_id in (${req.params.id})`;
	mysql.table('subject').where(where).delete().then(function (affectRows) {
		res.redirect('/admin/subject/subject_list');
	});
}

module.exports.subjectEdit = function(req,res){
	mysql.table('subject').where({sub_id:req.params.id}).find().then(function(subData){
				xtpl.renderFile(
					path.join(__dirname,'../views/admin/subject_edit.html'),
					{
						page:'subjectEdit',
						subList:subData,
					},  
					function(err,content){
						res.setHeader('Content-Type','text/html;charset=utf-8');
						res.send(content);
					}
				);
		}).catch(function(err){
			console.log(err)
		})
}

module.exports.subEditData = function(req,res){
	var where = `sub_id!='${req.params.id}' and sub_name='${req.body.name}'`;
	mysql.table('subject').field('sub_id').where(where).find().then(function(result){
		if(JSON.stringify(result)=='{}'){
			var data = {
				sub_name: req.body.name,
				sub_score: req.body.score,
			}
			mysql.table('subject').where({sub_id:req.params.id}).update(data).then(function(){
				console.log('修改成功')
				res.redirect('/admin/subject/subject_list');
			}).catch(function(err){
				console.log(err)
			})
		}else{
			baseCtr.sendMsg('该学科已存在',res);
		}
	}).catch(function(err){
		console.log(err)
	})
}